package com.usabb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.AmenityRelationDO;
import com.usabb.enums.BizTypeEnum;
import com.usabb.pojo.vo.AmenityAddVO;
import com.usabb.service.AmenityRelationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <pre>
 * 配套关联表
 * </pre>
 * <small> 2019-08-19 18:03:26 | Aron</small>
 */
@Slf4j
@Controller
@RequestMapping("/usabb/amenityRelation")
public class AmenityRelationController extends BaseController {
    @Autowired
    private AmenityRelationService amenityRelationService;

    @GetMapping()
    @RequiresPermissions("usabb:amenityRelation:amenityRelation")
    String AmenityRelation() {
        return "usabb/amenityRelation/amenityRelation";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("usabb:amenityRelation:amenityRelation")
    public Result<Page<AmenityRelationDO>> list(AmenityRelationDO amenityRelationDTO) {
        Wrapper<AmenityRelationDO> wrapper = new EntityWrapper<>(amenityRelationDTO).orderBy("id", false);
        Page<AmenityRelationDO> page = amenityRelationService.selectPage(getPage(AmenityRelationDO.class), wrapper);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("usabb:amenityRelation:add")
    String add() {
        return "usabb/amenityRelation/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("usabb:amenityRelation:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        AmenityRelationDO amenityRelation = amenityRelationService.selectById(id);
        model.addAttribute("amenityRelation", amenityRelation);
        return "usabb/amenityRelation/edit";
    }

    @Log("添加配套关联表")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("usabb:amenityRelation:add")
    public Result<String> save(AmenityRelationDO amenityRelation) {
        amenityRelationService.insert(amenityRelation);
        return Result.ok();
    }

    @Log("添加 or 编辑 配套关联表-ids")
    @ResponseBody
    @PostMapping("/save-ids")
    @RequiresPermissions("usabb:amenityRelation:add")
    public Result<String> saveIds(AmenityAddVO addVO) {
        //TODO: addVO
        amenityRelationService.saveOrUpdate(addVO);
        return Result.ok();
    }

    @Log("添加 or 编辑 民宿配套关联表-ids")
    @ResponseBody
    @PostMapping("/save-ids-homestay")
    @RequiresPermissions("usabb:amenityRelation:add")
    public Result<String> saveIdsHomeStay(AmenityAddVO addVO) {
        //TODO: addVO
        amenityRelationService.saveOrUpdate(addVO);
        return Result.ok();
    }

    @Log("修改配套关联表")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("usabb:amenityRelation:edit")
    public Result<String> update(AmenityRelationDO amenityRelation) {
        boolean update = amenityRelationService.updateById(amenityRelation);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除配套关联表")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("usabb:amenityRelation:remove")
    public Result<String> remove(Long id) {
        amenityRelationService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除配套关联表")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("usabb:amenityRelation:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        amenityRelationService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
